package com.sdry.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * @program: HYWLV_1.0
 * @description: 通用数据处理
 * @author: cb
 * @create: 2019-09-14 21:20
 **/
public class BaseController implements Serializable {

    public static Map<String,Object> map = Collections.synchronizedMap(new HashMap<>());

    public final static Logger logger = LoggerFactory.getLogger(BaseController.class);

    /**
     * 返回的结果集
     * @param list 集合
     * @param total 总数
     * @return TableDataInfo
     */
    public TableDataInfo getDataTable(List<?> list, Long total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCount(total);
        rspData.setCode(0);
        if (list.size() == 0 || list == null) {
            rspData.setData(new ArrayList<>());
        }else {
            rspData.setData(list);
        }
        return rspData;
    }

    /**
     * 返回的结果集报错
     * @return TableDataInfo
     */
    public TableDataInfo getDataTableError() {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setData(new ArrayList<>());
        return rspData;
    }

    /**
     * App接口返回的结果集
     * @param status 状态 0-失败，1-成功
     * @param data 结果集
     * @param message 描述
     * @return ResultAppInfo
     */
    public ResultAppInfo getResultAppInfo(String status, List<?> data, String message) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getParameter("token");
        ResultAppInfo resultAppInfo = new ResultAppInfo();
        resultAppInfo.setStatus(status);
        resultAppInfo.setToken(token);
        if ("0".equals(status)){
            resultAppInfo.setMessage(message);
        }else {
            resultAppInfo.setData(data);
        }
        return resultAppInfo;
    }

    public Map<String,Object> getResultAppLogin(String status, Object object, String message,String token) {
        Map<String,Object> map = new HashMap<>();
        map.put("status",status);
        if ("0".equals(status)){
            map.put("message",message);
        }else {
            map.put("data",object);
        }
        map.put("token",token);
        return map;
    }


}
